package com.example.memosystem;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.example.memosystem.activity.AddActivity;
import com.example.memosystem.activity.ChangePwdActivity;
import com.example.memosystem.activity.ManageActivity;
import com.example.memosystem.activity.MesActivity;
import com.example.memosystem.activity.RegisterActivity;
import com.example.memosystem.dao.impl.AdminDao;
import com.example.memosystem.util.DBUtil;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class MainActivity extends AppCompatActivity {

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        //连接数据库
        DBUtil dbUtil = new DBUtil(MainActivity.this);
        SQLiteDatabase db = dbUtil.getWritableDatabase();
        DBUtil.db = db;

        //进入注册页面
        Button register=findViewById(R.id.btn2);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "注册页面", Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(MainActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

        //登录按钮
        EditText account = findViewById(R.id.edit1);
        EditText password = findViewById(R.id.edit2);

        Button login_btn1 = findViewById(R.id.btn1);
        login_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String log_account = account.getText().toString().trim();
                String log_password = password.getText().toString().trim();

                if(log_account.isEmpty()){
                    Toast.makeText(MainActivity.this, "请输入账号", Toast.LENGTH_SHORT).show();
                }else if(log_password.isEmpty()){
                    Toast.makeText(MainActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                }else {
                    AdminDao adminDao = new AdminDao();
                    int a = adminDao.loginAdmin(log_account, log_password);
                    if(a == 0)
                    {
                        Toast.makeText(MainActivity.this, "账号或者密码不正确", Toast.LENGTH_SHORT).show();
                    }else {
                        Intent intent = new Intent(MainActivity.this, ManageActivity.class);
                        intent.putExtra("account", log_account);
                        startActivity(intent);
                        Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    }

                }

            }
        });


    }
}